﻿var _____CallSocketCompentType = '';
var _____CallSocketCompentId = '';
var _____ChemDrawSocketImgBase64 = '';
var _____ChemDrawSocketCdx = '';
var _____ChemDrawSocketMol = '';

var _____ChemDrawSocket = $.websocket({

    domain: "127.0.0.1",   //这是与服务器的域名或IP

    port: 8181,                  //这是服务器端口号

    protocol: "text",            //这东西可有可无,组合起来就是 ws://www.qhnovel.com:8080/test

    onOpen: function (event) {

        // alert("已经与服务端握手,onOpen可省略不写"); 
    },

    onError: function (event) {

        // alert("发生了错误,onError可省略不写"); 
    },

    onSend: function (msg) {
        // alert("发送数据额外的代码,可省略不写"); 
    },

    onMessage: function (result, nTime)
    {
        // alert("从服务端收到的数据:" + result);
        if (result.length > 6)
        {
            var s1 = result.substr(0, 6);
            var s2 = result.substr(6);
            switch (s1)
            {
                case 'GetImg':
                    _____ChemDrawSocketImgBase64 = s2;
                    break;
                case 'GetCdx':
                    _____ChemDrawSocketCdx = s2;
                    break
                case 'GetMol':
                    _____ChemDrawSocketMol = s2;
                    break;
                default:
                    alert(result);
                    break;
            }
            _____ChemDrawSocketCallBack();
        }
        //alert("最近一次发送数据到现在接收一共使用时间:" + nTime); 
    }
});

function _____ChemDrawSocketCallBack()
{
    if (_____ChemDrawSocketImgBase64.length > 0 && _____ChemDrawSocketCdx.length > 0)
    {
        switch (_____CallSocketCompentType)
        {
            case 'tlcCD':
                var tldDiv = Ext.getCmp(_____CallSocketCompentId);
                tldDiv.imgcdx = _____ChemDrawSocketCdx;
                tldDiv.imgbase64 = _____ChemDrawSocketImgBase64;
                tldDiv. showImg();
                break;
            case 'chemdraw':
                var _chemdrawPanel = Ext.getCmp(_____CallSocketCompentId);
                _chemdrawPanel.img = _____ChemDrawSocketImgBase64;
                _chemdrawPanel.cdx = _____ChemDrawSocketCdx;
                _chemdrawPanel.mol = _____ChemDrawSocketMol;
                _chemdrawPanel.showImg();
                break;
            default:
                break;
        }
    }

}



function getKetcher() {
    var frame = null;

    if ('frames' in window && 'ketcherFrame' in window.frames)
        frame = window.frames['ketcherFrame'];
    else
        return null;

    if ('window' in frame)
        return frame.window.ketcher;
}


function getJsDraw() {
    var frame = null;

    if ('frames' in window && 'jsdrawFrame' in window.frames)
        frame = window.frames['jsdrawFrame'];
    else
        return null;

    if ('window' in frame)
        return frame.window.JsDraw;
}
//-------------------图片显示----------------------
showImg = function (compId, imgBase64, padding, align) {


    if (imgBase64 == null || imgBase64.length < 10) {
        return;
    }

    if (padding == null) {
        padding = 0;
    }
    if (align == null) {
        align = 'center';
    }


    var sampleStructImgHtml = Ext.getCmp(compId);
    //////////////////////////////
    var iwidth = sampleStructImgHtml.getWidth() - padding * 2;
    var iheight = sampleStructImgHtml.getHeight() - 60;
    console.log('区域尺寸: ' + iwidth + "×" + iheight);

    var image = new Image();
    image.onload = function () {


        console.log('原图尺寸: ' + image.width + "×" + image.height);

        var this_imgWidth = 0;
        var this_imgHeight = 0;

        if (image.width > 0 && image.height > 0) {
            if (image.width / image.height >= iwidth / iheight) {
                if (image.width > iwidth) {
                    this_imgWidth = iwidth;
                    this_imgHeight = (image.height * iwidth) / image.width;
                }
                else {
                    this_imgWidth = image.width;
                    this_imgHeight = image.height;
                }
            }
            else {
                if (image.height > iheight) {
                    this_imgHeight = iheight;
                    this_imgWidth = (image.width * iheight) / image.height;
                } else {
                    this_imgWidth = image.width;
                    this_imgHeight = image.height;
                }
            }

            console.log('图片显示尺寸: ' + this_imgWidth + "×" + this_imgHeight);
            if (this_imgHeight < 30) {
                this_imgHeight = 30;
            }
        }

        var xtpl = new Ext.XTemplate("<table border='0' width='100%'><tr><td  align='" + align + "' valign='middle'><img src='data:image/png;base64,{imgData}' height='{height}' width='{width}' border='{border}'/></td></tr></table>");

        var html = xtpl.applyTemplate({
            imgData: imgBase64,
            height: this_imgHeight,
            width: this_imgWidth,
            border: false
        });
        sampleStructImgHtml.update(html);
    };

    image.src = 'data:image/png;base64,' + imgBase64;




}